package rd.hardcore.ui.tools 
{
	import caurina.transitions.Tweener;
	import flash.display.GradientType;
	import flash.display.SpreadMethod;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.geom.Matrix;
	import flash.geom.Rectangle;
	import flash.ui.Mouse;
	import flash.ui.MouseCursor;
	import rd.hardcore.engine.sound.HardcoreGhettoSoundSystem;
	import rd.hardcore.engine.sound.HardcoreSoundModifier;
	import rd.hardcore.events.SoundModifierEvent;
	/**
	 * ...
	 * @author Fazermokeur
	 */
	public class AcceleratorCursor extends AcceleratorCursor_FC
	{
		private var r:Rectangle = new Rectangle( -73, 0, 144, 0);
		private var checkBox:CheckBox;
		private var eightBitsBox:CheckBox;
		private var cursor : Cursor;
		
		public function AcceleratorCursor() 
		{
			addEventListener(Event.ADDED_TO_STAGE, init);
		}
		
		private function init(e:Event):void 
		{
			removeEventListener(Event.ADDED_TO_STAGE, init);
			
			graphics.lineStyle(1, 0x949494,.5);
			for (var i:int = 1; i < 10; i++) 
			{
				graphics.moveTo( -75 + i * 15, -5);
				graphics.lineTo( -75 + i * 15, 5);
			}
			graphics.endFill();

			addChild(bg);
			bg.addEventListener(MouseEvent.CLICK, handleBGClick);
			bg.buttonMode = true;

			cursor = new Cursor_FC();
			addChild(cursor);
			cursor.addEventListener(MouseEvent.MOUSE_DOWN, handleDown);
			
			checkBox = new CheckBox();
			addChild(checkBox);
			checkBox.x = -(width >> 1) + ( (width - checkBox.width) >> 1 );
			checkBox.y = 25;
			checkBox.addEventListener(MouseEvent.CLICK, handleCheckBox);
			
			/*	A   V O I R 	*/
			/*eightBitsBox = new CheckBox();
			addChild(eightBitsBox);
			eightBitsBox.x = -75;
			eightBitsBox.y = 10;
			eightBitsBox.buttonMode = true;
			eightBitsBox.select();
			eightBitsBox.addEventListener(MouseEvent.CLICK, handle8Bits);*/
		}
		
		public function handleWheel(e:MouseEvent):void 
		{			
			cursor.x = Math.max( Math.min(72, cursor.x + e.delta ) , -72 );
			
			onFrame();
			
			if (checkBox.checked)	Tweener.addTween(cursor, { x:0, time:1, onUpdate:onFrame, transition:"easeOutCubic" } );
		}
		
		
		private function handleBGClick(e:MouseEvent):void 
		{
			if(checkBox.checked)	handleCheckBox(null);
			Tweener.addTween(cursor, {x:bg.mouseX, time:.5, onUpdate:onFrame, transition:"easeOutCubic" } );
		}
		
		private function handle8Bits(e:MouseEvent):void 
		{
			eightBitsBox.select();
			if (eightBitsBox.checked) {
				HardcoreGhettoSoundSystem.PSEUDO_8BITS = true;
			}else {
				HardcoreGhettoSoundSystem.PSEUDO_8BITS = false;
			}
		}
		
		private function handleCheckBox(e:MouseEvent):void 
		{
			checkBox.select();
			if (checkBox.checked) {
				handleUp(null);
				dispatchEvent(new SoundModifierEvent(SoundModifierEvent.AUTO_RESSET, 0, true));
				HardcoreGhettoSoundSystem.AUTO_RESSET_SPEED = true;
			}else {
				dispatchEvent(new SoundModifierEvent(SoundModifierEvent.NO_AUTO_RESSET, 0, true));
				HardcoreGhettoSoundSystem.AUTO_RESSET_SPEED = false;
			}
		}
		
		private function handleDown(e:MouseEvent):void 
		{
			cursor.removeEventListener(MouseEvent.MOUSE_DOWN, handleDown);
			stage.addEventListener(MouseEvent.MOUSE_UP, handleUp);
			addEventListener(Event.ENTER_FRAME, onFrame);
			
			cursor.startDrag(false, r);
		}
		
		private function handleUp(e:MouseEvent):void 
		{
			cursor.addEventListener(MouseEvent.MOUSE_DOWN, handleDown);
			stage.removeEventListener(MouseEvent.MOUSE_UP, handleUp);
			removeEventListener(Event.ENTER_FRAME, onFrame);
			
			cursor.stopDrag();
			if (checkBox.checked)	Tweener.addTween(cursor, { x:0, time:1, onUpdate:onFrame, transition:"easeOutCubic" } );
		}
		
		private function onFrame(e:Event = null):void 
		{
			var ratio:Number = 1 + (cursor.x / 72);
			dispatchEvent(new SoundModifierEvent(SoundModifierEvent.CHANGE_SPEED, ratio,true));
		}
		
	}

}